package com.pm.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.pm.common.utils.DictUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.business.domain.EnterpriseAward;
import com.pm.business.service.IEnterpriseAwardService;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 企业获奖Controller
 * 
 * @author zxl
 * @date 2022-05-07
 */
@RestController
@RequestMapping("/business/enterprise_award")
public class EnterpriseAwardController extends BaseController
{
    @Autowired
    private IEnterpriseAwardService enterpriseAwardService;

    /**
     * 查询企业获奖列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseAward enterpriseAward)
    {
        startPage();
        List<EnterpriseAward> list = enterpriseAwardService.selectEnterpriseAwardList(enterpriseAward);
        return getDataTable(list);
    }

    /**
     * 查询企业获奖列表（小程序端）
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:list')")
    @GetMapping("/appList")
    public TableDataInfo appList(EnterpriseAward enterpriseAward)
    {
        startPage();
        List<EnterpriseAward> list = enterpriseAwardService.selectEnterpriseAwardList(enterpriseAward);
        TableDataInfo result = getDataTable(list);
        // 取第一张图片
        if(list!=null){
            list.forEach(cc->{
                if(cc.getAwardImage()!=null){
                    cc.setAwardImage(cc.getAwardImage().split(",")[0]);
                }
            });
        }
        return result;
    }

    /**
     * 导出企业获奖列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:export')")
    @Log(title = "企业获奖", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseAward enterpriseAward)
    {
        List<EnterpriseAward> list = enterpriseAwardService.selectEnterpriseAwardList(enterpriseAward);
        ExcelUtil<EnterpriseAward> util = new ExcelUtil<EnterpriseAward>(EnterpriseAward.class);
        util.exportExcel(response, list, "企业获奖数据");
    }

    /**
     * 获取企业获奖详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:query')")
    @GetMapping(value = "/{awardId}")
    public AjaxResult getInfo(@PathVariable("awardId") Long awardId)
    {
        return AjaxResult.success(enterpriseAwardService.selectEnterpriseAwardByAwardId(awardId));
    }
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:query')")
    @GetMapping(value = "/getAppInfo/{awardId}")
    public AjaxResult getAppInfo(@PathVariable("awardId") Long awardId)
    {
        EnterpriseAward enterpriseAward = enterpriseAwardService.selectEnterpriseAwardByAwardId(awardId);
        String awardType = enterpriseAward.getAwardType();
        if(awardType!=null && awardType.length()>0){
            enterpriseAward.setAwardType(DictUtils.getDictLabel("pm_award_category_type",awardType));
        }
        return AjaxResult.success(enterpriseAward);
    }

    /**
     * 新增企业获奖
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:add')")
    @Log(title = "企业获奖", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterpriseAward enterpriseAward)
    {
        return toAjax(enterpriseAwardService.insertEnterpriseAward(enterpriseAward));
    }

    /**
     * 修改企业获奖
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:edit')")
    @Log(title = "企业获奖", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnterpriseAward enterpriseAward)
    {
        return toAjax(enterpriseAwardService.updateEnterpriseAward(enterpriseAward));
    }

    /**
     * 删除企业获奖
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_award:remove')")
    @Log(title = "企业获奖", businessType = BusinessType.DELETE)
	@DeleteMapping("/{awardIds}")
    public AjaxResult remove(@PathVariable Long[] awardIds)
    {
        return toAjax(enterpriseAwardService.deleteEnterpriseAwardByAwardIds(awardIds));
    }
}
